package com.example.fengyungo.controller.admin;

import com.example.fengyungo.constant.MsgConstant;
import com.example.fengyungo.domain.pojo.User;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.UserService;
import com.example.fengyungo.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "风运Go管理端-用户管理")
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation("条件查询用户信息")
    @GetMapping("/conditions")
    public Result getUsersByCondition(User user) {
        List<User> users = userService.getUsersByCondition(user);
        return Result.success(users);
    }

    @ApiOperation("查询所有用户信息")
    @GetMapping("/allUsers")
    @Cacheable(value = "user", key = "'allUsers'")
    public Result<List<User>> getAllUsers() {
        List<User> allUsers = userService.getAllUsers();
        return Result.success(allUsers);
    }

    /**
     * 修改用户信息
     *
     * @param user User
     * @return Result
     */
    @ApiOperation("修改用户信息")
    @PutMapping("/updateUser")
    public Result updateUser(User user) {
        Integer res = userService.updateUserById(user);
        if (res > 0) {
            // 清除缓存
            redisUtil.cleanCache("fengyungouser*");
            return Result.success();
        }
        return Result.error(MsgConstant.UPDATE_FAIL);
    }

}
